package com.base.starter.config.shiro;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.InvalidRequestFilter;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.apache.shiro.web.filter.mgt.FilterChainManager;

import javax.servlet.Filter;
import java.util.Map;

/**
* @Description: 自定义Shiro过滤链
* @Param:  
* @return:  
* @Author: zhou minghao
* @Date: 9:44 2024/4/17 
*/ 
public class CommonShiroFilterFactoryBean extends ShiroFilterFactoryBean {

    @Override
    protected FilterChainManager createFilterChainManager() {
        //调用父类的createFilterChainManager()方法获取默认的createFilterChainManager实例
        //createFilterChainManager是一个负责管理过滤器链的组件，主要定义哪些URL路径通过哪些过滤器
        FilterChainManager manager = super.createFilterChainManager();

        //获取所有过滤器中的映射
        Map<String, Filter> filterMap = manager.getFilters();
        Filter invalidRequestFilter = filterMap.get(DefaultFilter.invalidRequest.name());
        if (invalidRequestFilter instanceof InvalidRequestFilter) {
            // 设置false跳过URL携带中文400，servletPath中文校验
            //允许URL中包含非ASCII字符，例如中文字符
            ((InvalidRequestFilter) invalidRequestFilter).setBlockNonAscii(false);
        }
        return manager;
    }
}
